import {
  checkLogin
} from './auth.js'
new Vue({
  el: '#app',
  data: {
    IS_LOGIN: null,
    searchKey: '', // 搜索的关键词
    // 订单的id
    orderId: null,
    // 订单信息
    orderInfo: null,
    totalPrice: 0
  },
  async created() {
    this.IS_LOGIN = await checkLogin()
    if (this.IS_LOGIN == null) {
      location.href = '/'
    }
    this.getQueryParam()
    await this.getOrderInfo()
  },
  methods: {
    // 确认付款
    async payOrder() {
      this.$message.success('支付成功')
      const res = await request.get('/alipay/pay', {
        params: {
          oid: this.orderId,
          totalPrice: this.totalPrice
        }
      })
      console.log(res.data)
      const htmlContent = res.data
      // 用支付页面内容替换当前页面内容
      document.open();
      document.write(htmlContent);
      document.close();
    },
    // 获取订单的详细信息
    async getOrderInfo() {
      const res = await request.get('/order/getOrderById', {
        params: {
          orderId: this.orderId
        }
      })
      this.orderInfo = res.data
      this.totalPrice = this.orderInfo.totalPrice
    },
    // 获取参数
    getQueryParam() {
      this.orderId = location.search.substring(location.search.lastIndexOf('=') + 1)
      console.log(this.orderId);
    },
    // 查找商品
    searchProduct() {
      console.log('查找商品:');
      location.href = `../web/search.html?searchKey=${this.searchKey}`
    },
    // 退出登录
    logout() {
      localStorage.removeItem('token')
      location.href = '/'
    },
  }
})